﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
using bmapi.HelperClass;
using bmapi.Models.DataEntity;
using Newtonsoft.Json.Linq;
using static bmapi.Models.ShowModel;
using System.Web.Script.Serialization;
using System.Xml;
using Jose;
using static bmapi.HelperClass.AssistFun;
using ICSharpCode.SharpZipLib.Zip;

namespace bmapi.Controllers
{
    public class PageController : BuyiController
    {

        #region  工序的相关操作

        /// <summary>
        /// 获取成衣所有工序
        /// </summary> 
        /// <returns></returns>
        protected List<Item> GetCYProcess()
        {
            string rootPath = HttpContext.Current.Server.MapPath("~/HelpFile");
            //string filepath = HttpContext.Current.Server.MapPath("/HelpFile");
            string jsonText = File.ReadAllText(rootPath + "//ChengYi.json");
            jsonText = jsonText.Replace("\r\n", "").Trim();
            jsonText = jsonText.Replace(" ", "");
            JavaScriptSerializer js = new JavaScriptSerializer();
            List<Item> chengyi = js.Deserialize<List<Item>>(jsonText);
            return chengyi;
        }
        /// <summary>
        /// 获取团订所有工序 
        /// </summary>  
        /// <returns></returns>
        protected List<Item> GetTDProcess()
        {
            string rootPath = HttpContext.Current.Server.MapPath("~/HelpFile");
            //string filepath = HttpContext.Current.Server.MapPath("/HelpFile");
            string jsonText = File.ReadAllText(rootPath + "//TuanDing.json");
            jsonText = jsonText.Replace("\r\n", "").Trim();
            jsonText = jsonText.Replace(" ", "");

            JavaScriptSerializer js = new JavaScriptSerializer();
            List<Item> tuanding = js.Deserialize<List<Item>>(jsonText);
            return tuanding;
        }
        /// <summary>
        /// 用过工序编码匹配对应的工序名称 
        /// </summary>
        /// <param name="proCode">工序编码</param>
        protected List<string> MatchProNameByProCode(List<int> proCode)
        {
            List<string> proName = new List<string>();
            List<Item> AllProName = GetCYProcess();
            AllProName.AddRange(GetTDProcess());
            for (int i = 0; i < proCode.Count; i++)
            {
                for (int j = 0; j < AllProName.Count; j++)
                {
                    if (proCode[i] == AllProName[j].code)
                    {
                        proName.Add(AllProName[j].name);
                        break;
                    }
                    else if (proCode[i] == 0)
                    {
                        proName.Add("待定");
                        break;
                    }
                }
            }
            return proName;
        }

        #endregion


        /// <summary>
        /// 压缩文件夹
        /// </summary>
        /// <param name="_OfloderPath"></param>
        /// <param name="zos"></param>
        /// <param name="_floderPath"></param>
        protected void ZipFloder(string _OfloderPath, ZipOutputStream zos, string _floderPath)
        {
            foreach (FileSystemInfo item in new DirectoryInfo(_floderPath).GetFileSystemInfos())
            {
                if (Directory.Exists(item.FullName))
                {
                    ZipFloder(_OfloderPath, zos, item.FullName);
                }
                else if (File.Exists(item.FullName))//如果是文件  
                {
                    DirectoryInfo ODir = new DirectoryInfo(_OfloderPath);
                    string fullName2 = new FileInfo(item.FullName).FullName;
                    string path = ODir.Name + fullName2.Substring(ODir.FullName.Length, fullName2.Length - ODir.FullName.Length);//获取相对目录  
                    FileStream fs = File.OpenRead(fullName2);
                    byte[] bts = new byte[fs.Length];
                    fs.Read(bts, 0, bts.Length);
                    ZipEntry ze = new ZipEntry(path);
                    zos.PutNextEntry(ze);             //为压缩文件流提供一个容器  
                    zos.Write(bts, 0, bts.Length);  //写入字节 
                    fs.Dispose();
                    fs.Close();
                }
            }
        }

        

        /// <summary>
        /// 获取流水线名称
        /// </summary>
        /// <param name="plid">流水线ID</param>
        /// <returns></returns>
        protected string GetPLNameBvPlid(int plid)
        {
            string sql = "select Name from PL_tb where id=@plid";
            SQLCommand sc = new SQLCommand(sql,"@plid",plid);
            string name = sc.ExecuteScalar();
            if (name == null)
                return "";
            else
                return name;
        }
        /// <summary>
        /// 获取流程当前的工序
        /// </summary>
        /// <param name="pcid"> 流程id</param>
        /// <returns></returns>
        protected int GetPcStep(int pcid)
        {
            string sql = "select step from PC_tb where pc_id=@pcid";
            SQLCommand sc = new SQLCommand(sql, "@pcid", pcid);
            string step = sc.ExecuteScalar();
            if (step == null)
                return 100;
            else
                return Convert.ToInt32(step);
            
        }
    }
}